import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

public class BeeSprite extends Sprite {

	protected Image imageLeft;
	protected Image imageRight;
	protected Image imageDeadLeft;
	protected Image imageDeadRight;

	protected float maxspeed;
	protected float speedx;
	protected float speedy;

	protected int compteur;
	protected int maxlimit;
	protected int speedLimit;
	protected int value;
	protected int beeScore;

	protected boolean flagx;
	protected boolean flagy;
	protected boolean isHit;
	protected boolean isEscape = false;
	protected boolean isNew = true;
	protected int bound;

	protected Random random;

	protected Gameloop gameloop;

	public int getValue() {
		return value;
	}

	public boolean getIsHit() {
		return isHit;
	}

	public void setIsHit(boolean isHit) {
		this.isHit = isHit;
	}

	public BeeSprite(Gameloop gl) {
		super();

		gameloop = gl;

		imageLeft = ImageLoader.get().read("/Images/BeeLeft.png");
		imageRight = ImageLoader.get().read("/Images/BeeRight.png");

		imageDeadLeft = ImageLoader.get().read("/Images/DeadBeeLeft.png");
		imageDeadRight = ImageLoader.get().read("/Images/DeadBeeRight.png");

		width = 60;
		height = 60;
		random = new Random();

		x = random.nextFloat() > 0.5 ? - width : gameloop.getWidth();
		y = (int) Math.round(random.nextFloat() * (gameloop.getHeight() - 80)
				- height - 40);

		maxspeed = 8.0f;
		speedx = 1.0f;
		speedy = 1.0f;

		compteur = 0;
		maxlimit = 40;
		speedLimit = random.nextInt(maxlimit) + 1;

		flagx = random.nextBoolean();
		flagy = random.nextBoolean();

		isHit = false;
		value = 3;
	}

	public void update() {

		if (isHit) {
			deadpath();
			return;
		}

		if (++compteur % speedLimit == 0) {
			flagy = !flagy;

			// celle qui fait chier
			speedLimit = random.nextInt(maxlimit) + 1;
			speedx = random.nextFloat() * maxspeed;
			speedy = random.nextFloat() * maxspeed;
		}

		if (x >= gameloop.getWidth() - width || x < 0) {
			if (!isNew) {
				bound++;
			}
		}
		else {
			isNew = false;
		}

		if (bound > 2) {
			isEscape = true;
		} else {
			flagx = (x >= gameloop.getWidth() - width) ? false : (x < 0) ? true
					: flagx;
		}

		flagy = (y >= (gameloop.getHeight()) - height - 25) ? false
				: (y < 80) ? true : flagy;

		x = (int) (x + (speedx * (flagx ? 1 : -1)));
		y = (int) (y + (speedy * (flagy ? 1 : -1)));

	}

	public void deadpath() {

		speedx = 3.7f;
		speedy = 1.5f;

		if (++compteur % speedLimit == 0) {

			speedLimit = random.nextInt(maxlimit) + 1;

			flagx = random.nextBoolean();
			compteur = 0;
		}

		flagx = (x >= gameloop.getWidth() - width) ? false : (x < 0) ? true
				: flagx;

		x = (int) (x + (speedx * (flagx ? 1 : -1)));

		y = (int) (y + (speedy * 3.0f));
	}

	public void draw(Graphics g) {

		if (!flagx && !isHit) {
			g.drawImage(imageLeft, x, y, width, height, null);
		} else if (flagx && !isHit) {
			g.drawImage(imageRight, x, y, width, height, null);
		} else if (isHit && !flagx) {
			g.drawImage(imageDeadLeft, x, y, width, height, null);
		} else {
			g.drawImage(imageDeadRight, x, y, width, height, null);
		}
	}

}
